//
//  YHTabBar.swift
//  WeiBoYH
//
//  Created by yanhui on 2017/1/4.
//  Copyright © 2017年 yanhui. All rights reserved.
//

import UIKit

class YHTabBar: UITabBar {
    
    //定义一个闭包
    var custonClosure: (() -> ())?
    

    //初始化添加自定义控件
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setUpUI()
    }
    
    
   private func setUpUI(){
        self.addSubview(customBtn)
    
        customBtn.addTarget(self, action: #selector(didClickButton), for: .touchUpInside)
        
    }
    
    
  @objc private func didClickButton(){
        
        custonClosure?()
        
    }
    
    
    
    
    //布局子视图方法中修改子视图的frame
        override func layoutSubviews(){
            super.layoutSubviews()
            let w = UIScreen.main.bounds.width / 5
            let h = bounds.height
    
            var index = 0
            //遍历
            for subview in subviews{
                
                if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                    subview.frame = CGRect(x: CGFloat(index) * w, y: 0, width: w, height: h)
                    if index == 1 {
                        
                        index += 1
                    }
                    
                    index += 1
                }
           
            }
        customBtn.center = CGPoint(x: self.center.x, y: h * 0.5)
        customBtn.bounds.size = CGSize(width: w, height: h)
        
        }
    
    lazy var customBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        return btn
    }()
    
    
}
